<template>
	<view>
		<view class="mapAddress_empty" v-if="this.mapAddress_empty">
			<u-empty mode="address" icon="http://cdn.uviewui.com/uview/empty/address.png">
			</u-empty>
		</view>
		<view class="mapAddressList" v-if="!this.mapAddress_empty">
			<u-cell-group>
				<navigator :url="`/pages/address/addAddress/addAddress?id=${item.id}`"  v-for="(item,index) in addressList" :key="item.id"> 
				<u-cell size="large" >
					<view slot="title" class="u-slot-title">
						<text>收件人：</text>{{item.name}}
					</view>
					<view slot="value" class="u-slot-title">
						<text>收货地址：</text>{{item.region}}
						<p> {{item.address}}</p>
						<u-tag text="默认"  size="mini" style="width: 120rpx;" icon="map" v-if="item.isDefault" ></u-tag>
					</view>
					<view slot="label" class="u-slot-title">
						<text>联系电话：</text>{{item.phone}}
					</view>
				</u-cell>
				</navigator>
			</u-cell-group>
		</view>
		<button type="default" style="margin: 40rpx;" @click="addAddressHandler">添加地址</button>
	</view>
</template>

<script>
	import {
		GetUserAddressAPI
	} from "../../services/api.js"
	import {
		gettoken
	} from "../../utils/tools.js"
	export default {
		data() {
			return {
				mapAddress_empty: true,
				addressList: [],

			}
		},
		methods: {
			getUserAddress() {
				GetUserAddressAPI().then((res) => {
					if (res?.data?.length > 0) {
						this.mapAddress_empty = false
						this.addressList = res.data.map((item) => {
							return {
								name: item.name,
								region: item.region,
								address: item.address,
								phone: item.phone,
								isDefault:item.isDefault,
								id:item.id
							}
						})
					} else this.mapAddress_empty = true

				})
			},
			addAddressHandler() {
				uni.navigateTo({
					url: "/pages/address/addAddress/addAddress"
				})
			},
		},
		onShow() {
			this.getUserAddress()
			if (!gettoken()) {
				this.isShow = true
				uni.navigateTo({
					url: "/pages/login/login"
				})
			}
		}
	}
</script>

<style>
text{
	font-size: 30rpx;
	font-weight: 900;
	margin: 10rpx 0;
	display: block;
}
</style>